package Main;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

import Equasion.Equasion;


public class Graph extends JComponent {
	public static final int graphXSize = 500;
	public static final int graphYSize = 500;
	public static double period = .01;
	
	public Graph()
	{
		
	}
	
	@Override
	public void paint(Graphics g)
	{
		g.setColor(Color.white);
		g.fillRect(0, 0, graphXSize, graphYSize);
		drawAxis(g);
		int i = 0;
		for(Equasion e:Main.equasionList)
		{
			if(i == 0)
				g.setColor(Color.BLUE);
			else
				changeColor(g);
			e.drawEquasion(g,period,graphXSize,graphYSize);
			i++;
		}
	}
	
	public void drawAxis(Graphics g)
	{
		g.setColor(Color.black);
		g.drawLine(graphXSize/2, 0, graphXSize/2, graphYSize);
		g.drawLine(0, graphYSize/2, graphXSize, graphYSize/2);
		int dotPeriod = graphYSize/11;
		for(int i = -5;i<=5;i++)
		{
			if (i != 0)
			{
				g.drawLine(graphXSize/2-2, dotPeriod*i+graphYSize/2, graphXSize/2+2, dotPeriod*i+graphYSize/2);
				g.drawString(""+(i*(Math.abs(Main.yMin)+Math.abs(Main.yMax))/11), graphXSize/2+4, dotPeriod*i+graphYSize/2+6);
			}
		}
		
		for(int i = -5;i<=5;i++)
		{
			if (i != 0)
			{
				g.drawLine(i*dotPeriod+graphXSize/2, graphYSize/2+2, i*dotPeriod+graphXSize/2, graphYSize/2-2);
				g.drawString(""+(i*(Math.abs(Main.xMin)+Math.abs(Main.xMax))/11), dotPeriod*i+graphXSize/2-3, graphYSize/2+16);
			}
		}
	}
	
	private void changeColor(Graphics g)
	{
		g.setColor(new Color((int)(Math.random()*255 + .5),(int)(Math.random()*255 + .5),(int)(Math.random()*255 + .5)));
	}
}
